package corej.course.collection;

import java.util.*;

public class TestStudentSort {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        List students = new ArrayList();
        Student s1 = new Student("Liucy");
        Student s2 = new Student("Huxz");
        Student s3 = new Student("George");

        s1.addScore("C++", 100);
        s1.addScore("CoreJava", 90);
        s1.addScore("Web", 80);
        s1.addScore("EJB", 50);

        s2.addScore("C++", 80);
        s2.addScore("CoreJava", 100);
        s2.addScore("Web", 90);
        s2.addScore("EJB", 85);

        s3.addScore("C++", 100);
        s3.addScore("CoreJava", 100);
        s3.addScore("Web", 100);
        s3.addScore("EJB", 100);

        students.add(s1);
        students.add(s2);
        students.add(s3);

        Collections.sort(students);

        Iterator it = students.iterator();
        while (it.hasNext()) {
            Object o = it.next();
            Student s = (Student) o;
            System.out.println(s.getName() + ":" + s.getMark());
        }

    }
}

class Student implements Comparable {
    String name;

    // key��String �γ��� value�� Double �γ̳ɼ�
    Map courses = new HashMap();

    double totalMark = 0;

    public Student(String name) {
        this.name = name;
    }

    @SuppressWarnings("unchecked")
    public void addScore(String course, double mark) {
        this.courses.put(course, new Double(mark));
        this.totalMark += mark;
    }

    public String getName() {
        return this.name;
    }

    // ���ѧ���ܷ�
    public double getMark() {
        /*
           * System.out.println("���� "+name+" ���ܷ�"); Collection
           * values=this.courses.values(); double result=0; Iterator
           * it=values.iterator(); while(it.hasNext()){ Object o=it.next(); Double
           * d=(Double)o; result+=d.doubleValue(); }
           *
           * return result;
           */
        return this.totalMark;
    }

    public int compareTo(Object o) {
        Student s = (Student) o;
        if (this.getMark() > s.getMark())
            return -1;
        else if (this.getMark() < s.getMark())
            return 1;
        else
            return this.name.compareTo(s.name);
    }
}
